<!DOCTYPE html>
<html lang="zh-cmn-Hans">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
		<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
		<meta http-equiv="Pragma" content="no-cache" />
		<meta http-equiv="Expires" content="0" />
		<title>ChinaBang Awards 2017</title>
		<link rel="stylesheet" href="../style/weui.css" />
		<link rel="stylesheet" href="./example.css" />
	</head>

	<body ontouchstart>
		<div class="weui-toptips weui-toptips_warn js_tooltips">错误提示</div>
		<div class="container" id="container"></div>
		<div class="page" style="opacity:1">
			<div class="page__hd">
				<h1 class="page__title">
        			<img src="images/logo.png" alt="ChinaBang" height="21px" />
   				 </h1>
   				 <p class="page__desc"></p>
			</div>
			<div class="weui-grids" id="nomineeItem">
			</div>
		</div>
		
		<script src="js/util/jquery-1.8.3.min.js"></script>
		<script src="./weui.min.js"></script>
		<script src="js/util/configs.js"></script>
		<script src="js/util/restUrls.js"></script>
		<script src="js/util/utils.js"></script>
		<script type="text/javascript">
			$(function() {
				var winH = $(window).height();
				var title = utils.getUrlParam("title");
				$(".page__desc").html(title);
				var categorySpace = 10;
				var voterId = localStorage.getItem("voterId") == null ? utils.getUrlParam("voterId") : localStorage.getItem("voterId");
				var accessToken = localStorage.getItem("accessToken") == null ? utils.getUrlParam("accessToken") : localStorage.getItem("accessToken");
				var categoryId = utils.getUrlParam("categoryId");
				if(!voterId || !accessToken) {
					var link_ = getWeChatUrl(title, categoryId);
					console.log(link_)
					window.location.href = link_;
					return;
				} else {
					localStorage.setItem("voterId", voterId);
					localStorage.setItem("accessToken", accessToken);
				}
				$.post(restUrls.vote.queryItemByCategory + "?voterId=" + voterId + "&accessToken=" + accessToken + "&categoryId="+categoryId, {}, function(result) {
					if(result.errorCode == 0) {
						if(result.data && result.data.rsData) {
							var itemList = result.data.rsData;
							$("#nomineeItem").html("");
							for(var i = 0; i < itemList.length; i++) {
								var action = "";
								if(itemList[i].result == 0) {
									action = "投一票";
								} else {
									action = itemList[i].count + " 票";
								}
								var item = '<a href="javascript:void(0);" class="weui-grid">' +
									'<div class="weui-grid__icon show_detail"  data-id="'+itemList[i].id+'">' +
									'<img  src="' + getImageUrl(itemList[i].logo) + '" alt="" />' +
									'</div>' +
									'<p class="weui-grid__label">' + itemList[i].name + '</p>' +
									'<button type="button" class="weui-btn weui-btn_mini weui-btn_primary voteBtn">'+action+'</button>' +
									'<input type="hidden" class="vote_item" value='+itemList[i].id+'>'+
									'</a>';
								$("#nomineeItem").append(item);
							}
							
							$(".show_detail").on("click", function() {
								var itemId = $(this).attr("data-id");
								
								window.location.href = "show.html?itemId="+itemId+"&title="+title;
							});
							$(".voteBtn").on('click', function() {
								var itemId = $(this).next().val();
								$.post(restUrls.vote.poll + "?voterId=" + voterId + "&accessToken=" + accessToken, {itemId:itemId}, function(result) {
									if(result.errorCode == 0) {
										var item = result.data.rsData[0];
										var words = "感谢您为 "+item.name+" 投上第 "+ item.count+" 票";
										weui.alert(words);
										setTimeout(function() {window.location.href = 'show.html?title='+title+'&itemId='+result.data.rsData[0].id;}, 2000);
									} else if(result.errorCode == 10001) { 
										weui.alert("投票项目不存在");
									} else if(result.errorCode == 10002) { 
										weui.alert("投票未开始");
									} else if(result.errorCode == 10003) { 
										weui.alert("投票已结束");
									} else if(result.errorCode == 10004) {
										weui.alert("该项目已投过票!");
									} else if(result.errorCode == 10005) { 
										weui.alert("该奖项投票次数已达上限");
									} else if(result.errorCode == 10006) { 
										weui.alert("该投票活动不存在");
									} else if(result.errorCode == 10010) {
											weui.alert("用户被禁用");
									} else if(result.errorCode == 30001) { 
										var link_ = getWeChatUrl(title, categoryId);
										window.location.href = link_;
										return;
									} else if(result.errorCode == 90000) {
											weui.alert("请求失败");
									}
								}, "json");
							});
						}
					} else if(result.errorCode == 30001) { 
						var link_ = getWeChatUrl(title, categoryId);
						window.location.href = link_;
						return;
					} else if(result.errorCode == 90000) {
							weui.alert("请求失败");
					}

				}, "json");
			});

			function getImageUrl(imgName) {
				if(imgName == undefined || imgName == '') {
					return "";
				} else {
					return "http://angelnode.qiniudn.com/" + imgName + "?imageView2/1/w/169/h/169";
				}
			}
			
			function getWeChatUrl(title, itemId) {
				var jsonParam = {"itemId":itemId, "title":title};
				var params = "?params="+JSON.stringify(jsonParam);
				var encodeParams = encodeURI(params);
				return restUrls.weChat.prefix + "http%3A%2F%2F" + restUrls.vote.getItemPage +encodeParams + restUrls.weChat.last;
			}
		</script>
	</body>
</html>
